package cn.com.czpb.client.module.newspager;

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import cn.com.czpb.client.R;
import cn.com.czpb.client.app.BaseFragment;
import cn.com.czpb.client.model.newspager.NewsPagerEntity;
import cn.com.czpb.client.model.newspager.NewspagerBean;
import cn.com.czpb.client.module.newspager.adapter.NewspagerRecycleAdapter;
import cn.com.czpb.client.net.Factory;
import cn.com.czpb.client.utils.LogUtils;
import cn.com.czpb.client.utils.PageCtrl;

import butterknife.BindView;
import cn.com.czpb.client.utils.TUtils;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * Created by 猪猪侠 on 2017/10/18.
 */

public class NewspagerFragment extends BaseFragment implements BaseQuickAdapter.OnItemChildClickListener, View.OnClickListener {
    @BindView(R.id.recycler_id)
    RecyclerView recyclerId;

    @BindView(R.id.refresh_id)
    TextView refresh_id;
    private NewspagerRecycleAdapter adapter;
    private boolean flag =false;

    @Override
    public int setMyContentView() {
        return R.layout.fragment_newspager;
    }

    public static NewspagerFragment newInstance() {
        NewspagerFragment newspagerFragment = new NewspagerFragment();
        return newspagerFragment;
    }

    @Override
    public void initView() {
        adapter = new NewspagerRecycleAdapter(null);
        MyGridLayoutManager manager = new MyGridLayoutManager(activity,2);
        manager.setScrollEnabled(false);
        recyclerId.setLayoutManager(manager);
        recyclerId.setAdapter(adapter);
        adapter.setOnItemChildClickListener(this);
        refresh_id.setOnClickListener(this);

    }

    @Override
    public void initData() {
        getNewsPager();
    }

    //获取数字报列表
    private void getNewsPager() {
        Factory.provideHttpService()
                .getPagerList()
                .subscribeOn(Schedulers.io())
                .filter(new Func1<NewsPagerEntity, Boolean>() {
                    @Override
                    public Boolean call(NewsPagerEntity entity) {
                        LogUtils.i("flash--200-->" + entity.code);
                        return "200".equals(entity.code);
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<NewsPagerEntity>() {
                               @Override
                               public void call(NewsPagerEntity entity) {
                                   if (flag){
                                       TUtils.toast("刷新成功");
                                   }
                                   adapter.setNewData(entity.data);
                               }
                           }, new Action1<Throwable>() {
                               @Override
                               public void call(Throwable throwable) {
                                   LogUtils.i("flash--200-->失败" + throwable.toString());
                               }
                           }
                );
    }

    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
        NewspagerBean bean = (NewspagerBean) adapter.getItem(position);
        switch (view.getId()){
            case R.id.ll_newspager_id:
                PageCtrl.start2NewsPagerActivity(activity,bean.getUrl());
                break;
        }
    }

    @Override
    public void onClick(View view) {
        flag =true;
        getNewsPager();

    }
}
